package info.thereisonlywe.onenesspathfinder.ui;

import info.thereisonlywe.core.font.FontLibrary;
import info.thereisonlywe.core.project.ProjectConstants;
import info.thereisonlywe.core.project.ProjectLocalization;
import info.thereisonlywe.core.project.ProjectSettings;
import info.thereisonlywe.core.search.SearchModifier;
import info.thereisonlywe.core.toolkit.ArrayToolkit;
import info.thereisonlywe.core.ui.swing.extensions.listcellrenderer.TooltipCellRenderer;
import info.thereisonlywe.quran.Quran;
import info.thereisonlywe.quran.QuranicVerse;
import info.thereisonlywe.quran.bookmark.QuranicBookmark;
import info.thereisonlywe.quran.bookmark.QuranicBookmarkManager;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
import javax.swing.LayoutStyle.ComponentPlacement;

public class SearchConfigurationPanel extends JPanel {

	private final JComboBox<String> comboBox_2;

	private final JComboBox<String> comboBox_3;

	private final JComboBox<String> comboBox_4;

	private final JRadioButton radioButton;

	private final JRadioButton radioButton_2;
	private JPanel panel;
	private final ButtonGroup buttonGroup_SearchModifier = new ButtonGroup();
	private JRadioButton rdbtnNewRadioButton;
	private JRadioButton rdbtnNewRadioButton_1;
	private JRadioButton rdbtnNewRadioButton_2;
	private JRadioButton rdbtnNewRadioButton_3;
	private JSeparator separator;

	public SearchConfigurationPanel()
	{
		setPreferredSize(new Dimension(560, 94));

		String[] sNames = Quran.getSectionNames(ProjectSettings.getLocale()
				.getLanguage());

		JPanel panel_4 = new JPanel();
		panel_4.setLayout(new FlowLayout(FlowLayout.LEFT, 7, 0));

		radioButton = new JRadioButton(ProjectLocalization.getString(
				ProjectConstants.ONENESS_PATHFINDER_PROJECT,
				"SearchInSectionRange"));
		radioButton.setFont(FontLibrary.get(FontLibrary.DROID_SANS, Font.BOLD,
				11));
		radioButton.setSelected(true);
		panel_4.add(radioButton);

		JPanel panel_6 = new JPanel();
		panel_6.setLayout(new FlowLayout(FlowLayout.LEFT, 7, 0));

		radioButton_2 = new JRadioButton(ProjectLocalization.getString(
				ProjectConstants.ONENESS_PATHFINDER_PROJECT,
				"SearchInBookmarks"));
		radioButton_2.setFont(FontLibrary.get(FontLibrary.DROID_SANS,
				Font.BOLD, 12));
		panel_6.add(radioButton_2);
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout
				.setHorizontalGroup(groupLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								groupLayout
										.createSequentialGroup()
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addGap(2)
																		.addComponent(
																				getSeparator(),
																				GroupLayout.PREFERRED_SIZE,
																				562,
																				Short.MAX_VALUE))
														.addComponent(
																panel_4,
																GroupLayout.DEFAULT_SIZE,
																564,
																Short.MAX_VALUE)
														.addComponent(
																panel_6,
																GroupLayout.DEFAULT_SIZE,
																564,
																Short.MAX_VALUE)
														.addComponent(
																getPanel(),
																Alignment.TRAILING,
																GroupLayout.DEFAULT_SIZE,
																564,
																Short.MAX_VALUE))
										.addContainerGap()));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(
				Alignment.LEADING).addGroup(
				groupLayout
						.createSequentialGroup()
						.addComponent(getPanel(), GroupLayout.PREFERRED_SIZE,
								21, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(getSeparator(), 2, 2, 2)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel_4, GroupLayout.PREFERRED_SIZE, 27,
								GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panel_6, GroupLayout.PREFERRED_SIZE, 27,
								GroupLayout.PREFERRED_SIZE)
						.addContainerGap(GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));

		comboBox_4 = new JComboBox<>();
		comboBox_4.setFont(FontLibrary.get(FontLibrary.DROID_SANS, Font.BOLD,
				11));
		panel_6.add(comboBox_4);

		comboBox_2 = new JComboBox<>();
		comboBox_2.setModel(new DefaultComboBoxModel<String>(sNames));
		comboBox_2.setSelectedIndex(0);
		comboBox_2.setFont(FontLibrary.get(FontLibrary.DROID_SANS, Font.BOLD,
				11));
		panel_4.add(comboBox_2);

		JLabel label_1 = new JLabel(">>");
		panel_4.add(label_1);

		comboBox_3 = new JComboBox<>();
		comboBox_3.setModel(new DefaultComboBoxModel<String>(sNames));
		comboBox_3.setFont(FontLibrary.get(FontLibrary.DROID_SANS, Font.BOLD,
				11));
		comboBox_3.setSelectedIndex(Quran.SECTION_COUNT - 1);
		panel_4.add(comboBox_3);
		TooltipCellRenderer renderer = new TooltipCellRenderer();
		renderer.setTooltips(new String[] {
				ProjectLocalization.getString(
						ProjectConstants.ONENESS_PATHFINDER_PROJECT,
						"Search.Contains"),
				ProjectLocalization.getString(
						ProjectConstants.ONENESS_PATHFINDER_PROJECT,
						"Search.StartsWith"),
				ProjectLocalization.getString(
						ProjectConstants.ONENESS_PATHFINDER_PROJECT,
						"Search.EndsWith"),
				ProjectLocalization.getString(
						ProjectConstants.ONENESS_PATHFINDER_PROJECT,
						"Search.Exact") });
		ButtonGroup bgroup = new ButtonGroup();
		bgroup.add(radioButton_2);
		bgroup.add(radioButton);
		setLayout(groupLayout);
		initListeners();
	}

	private void initListeners()
	{

		rdbtnNewRadioButton.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				MainFrame.view_Windows.getSearchPanel().setSearchModifier(
						SearchModifier.CONTAINS);
				MainFrame.view_Windows.getSearchPanel().refreshResults();
			}
		});

		rdbtnNewRadioButton_1.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				MainFrame.view_Windows.getSearchPanel().setSearchModifier(
						SearchModifier.STARTS_WITH);
				MainFrame.view_Windows.getSearchPanel().refreshResults();
			}
		});

		rdbtnNewRadioButton_2.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				MainFrame.view_Windows.getSearchPanel().setSearchModifier(
						SearchModifier.ENDS_WITH);
				MainFrame.view_Windows.getSearchPanel().refreshResults();
			}
		});

		rdbtnNewRadioButton_3.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				MainFrame.view_Windows.getSearchPanel().setSearchModifier(
						SearchModifier.EXACT);
				MainFrame.view_Windows.getSearchPanel().refreshResults();
			}
		});

		radioButton.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				applySearchOptions();
			}
		});

		radioButton_2.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				applySearchOptions();
			}
		});

		comboBox_4.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				applySearchOptions();
			}
		});

		comboBox_2.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				if (comboBox_3.getSelectedIndex() < comboBox_2
						.getSelectedIndex())
				{
					comboBox_3.setSelectedIndex(comboBox_2.getSelectedIndex());
				}
				applySearchOptions();

			}
		});

		comboBox_3.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				if (comboBox_3.getSelectedIndex() < comboBox_2
						.getSelectedIndex())
				{
					comboBox_3.setSelectedIndex(comboBox_2.getSelectedIndex());
				}
				applySearchOptions();

			}
		});

	}

	public void refreshValues()
	{
		comboBox_4.setModel(new DefaultComboBoxModel<String>(ArrayToolkit
				.insertAtBeginning(QuranicBookmarkManager
						.getExistingBookmarkListNames(), ProjectLocalization
						.getString(ProjectConstants.ONENESS_PATHFINDER_PROJECT,
								"OpenBookmarks"))));
	}

	public void applySearchOptions()
	{
		boolean change = false;
		QuranicVerse[] filter = MainFrame.view_Windows.getSearchPanel()
				.getFilter();
		QuranicVerse[] newfilter = getFilter();

		if (!Arrays.equals(filter, newfilter))
		{
			change = true;
			MainFrame.view_Windows.getSearchPanel().setFilter(newfilter);
		}
		filter = null;
		newfilter = null;
		if (change)
		{
			MainFrame.view_Windows.getSearchPanel().refreshResults();
		}
	}

	public QuranicVerse[] getFilter()
	{
		if (radioButton.isSelected())
		{
			if (comboBox_2.getSelectedIndex() == 0
					&& comboBox_3.getSelectedIndex() == Quran.SECTION_COUNT - 1) return null;
			else return Quran.getVerses(comboBox_2.getSelectedIndex() + 1,
					comboBox_3.getSelectedIndex() + 1);
		}
		else if (radioButton_2.isSelected())
		{
			if (comboBox_4.getSelectedIndex() == 0)
			{
				QuranicBookmark[] bookmarks = UIDelegate.BookmarkPaneHook
						.getBookmarkList().getBookmarks();
				QuranicVerse[] bookmarkPositions = new QuranicVerse[bookmarks.length];
				for (int i = 0; i < bookmarks.length; i++)
				{
					bookmarkPositions[i] = bookmarks[i].getPosition();
				}
				bookmarks = null;
				return bookmarkPositions;
			}
			else
			{
				String s = (String) comboBox_4.getSelectedItem();
				if (s == null || s.isEmpty()) return null;
				QuranicBookmark[] bookmarks = QuranicBookmarkManager
						.getBookmarks(QuranicBookmarkManager
								.getBookmarkListFile(s));
				QuranicVerse[] bookmarkPositions = new QuranicVerse[bookmarks.length];
				for (int i = 0; i < bookmarks.length; i++)
				{
					bookmarkPositions[i] = bookmarks[i].getPosition();
				}
				bookmarks = null;
				return bookmarkPositions;
			}
		}

		return null;
	}

	protected JPanel getPanel()
	{
		if (panel == null)
		{
			panel = new JPanel();
			panel.setLayout(new FlowLayout(FlowLayout.LEFT, 7, 0));
			panel.add(getRdbtnNewRadioButton());
			panel.add(getRdbtnNewRadioButton_1());
			panel.add(getRdbtnNewRadioButton_2());
			panel.add(getRdbtnNewRadioButton_3());
		}
		return panel;
	}

	protected JRadioButton getRdbtnNewRadioButton()
	{
		if (rdbtnNewRadioButton == null)
		{
			rdbtnNewRadioButton = new JRadioButton(
					SearchModifier.CONTAINS.toString());
			rdbtnNewRadioButton.setSelected(true);
			rdbtnNewRadioButton.setFont(FontLibrary.get(FontLibrary.DROID_SANS,
					Font.BOLD, 11));
			buttonGroup_SearchModifier.add(rdbtnNewRadioButton);
		}
		return rdbtnNewRadioButton;
	}

	protected JRadioButton getRdbtnNewRadioButton_1()
	{
		if (rdbtnNewRadioButton_1 == null)
		{
			rdbtnNewRadioButton_1 = new JRadioButton(
					SearchModifier.STARTS_WITH.toString());
			rdbtnNewRadioButton_1.setFont(FontLibrary.get(
					FontLibrary.DROID_SANS, Font.BOLD, 11));
			buttonGroup_SearchModifier.add(rdbtnNewRadioButton_1);
		}
		return rdbtnNewRadioButton_1;
	}

	protected JRadioButton getRdbtnNewRadioButton_2()
	{
		if (rdbtnNewRadioButton_2 == null)
		{
			rdbtnNewRadioButton_2 = new JRadioButton(
					SearchModifier.ENDS_WITH.toString());
			rdbtnNewRadioButton_2.setFont(FontLibrary.get(
					FontLibrary.DROID_SANS, Font.BOLD, 11));
			buttonGroup_SearchModifier.add(rdbtnNewRadioButton_2);
		}
		return rdbtnNewRadioButton_2;
	}

	protected JRadioButton getRdbtnNewRadioButton_3()
	{
		if (rdbtnNewRadioButton_3 == null)
		{
			rdbtnNewRadioButton_3 = new JRadioButton(
					SearchModifier.EXACT.toString());
			rdbtnNewRadioButton_3.setFont(FontLibrary.get(
					FontLibrary.DROID_SANS, Font.BOLD, 11));
			buttonGroup_SearchModifier.add(rdbtnNewRadioButton_3);
		}
		return rdbtnNewRadioButton_3;
	}

	protected JSeparator getSeparator()
	{
		if (separator == null)
		{
			separator = new JSeparator();
		}
		return separator;
	}
}
